package com.shiguiwu.springboot3.feign;

import feign.Feign;
import feign.Target;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import java.util.function.Supplier;

/**
 * @description: 配置客户端
 * @author: stone
 * @date: Created by 2023/7/30 22:20
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.feign
 */
@Component
@Import(FeignClientsConfiguration.class)
public class GenOpenFeignFactory<V> {

    @Autowired
    private Decoder decoder;

    @Autowired
    private Encoder feignEncoder;

    /**
     * 第一种方式
     * @param urlSupplier
     * @param vClass
     * @return
     */
    public V genFeignDynamicUrlClient(Supplier<DynamicUrlInterceptor.OpenFeignDTO> urlSupplier, Class<V> vClass) {
        return Feign.builder()
                .requestInterceptor(new DynamicUrlInterceptor(urlSupplier))
                .contract(new SpringMvcContract())
                .encoder(feignEncoder)
                .decoder(decoder)
                .target(Target.EmptyTarget.create(vClass));
    }

    public V genFeignDynamicUrlClientV2(Class<V> vClass) {
        return Feign.builder()
                .encoder(feignEncoder)
                .decoder(decoder)
                .target(Target.EmptyTarget.create(vClass));
    }



}
